package com.echo.fm.content.systemAction;

import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.echo.fm.admin.auto.model.Admin;
import com.echo.fm.content.systemAction.model.SystemAction;
import com.echo.util.ErrorException;
import com.echo.util.ResultUtils;
import com.echo.util.impl.HeaderParser;
import com.echo.util.impl.ValidateUtils;

@Controller
public class SystemActionController {
	@Autowired
	@Qualifier("systemActionService")
	ISystemActionService systemActionService;

	@RequestMapping(value = "/web/systemAction/addSave")
	@ResponseBody
	Object addSystemAction(HttpServletRequest request, HttpServletResponse response) {
		try {
			// Admin detail = HeaderParser.getUser(request);
			// if (detail == null) {
			// throw new ErrorException(ResultUtils.WEB_DB_SERVER_ERROR_CODE);
			// }
			SystemAction systemAction = new SystemAction();
			systemAction.setId(UUID.randomUUID().toString().replaceAll("-", ""));
			String superId = request.getParameter("super_id");
			if (ValidateUtils.isNotEmpty(superId)) {
				systemAction.setSuper_id(superId);

			}
			String url = request.getParameter("url");
			if (ValidateUtils.isNotEmpty(url)) {
				systemAction.setUrl(url);
			}
			String name = request.getParameter("name");
			if (ValidateUtils.isNotEmpty(name)) {
				systemAction.setName(name);
			}
			String intro = request.getParameter("intro");
			if (ValidateUtils.isNotEmpty(intro)) {
				systemAction.setIntro(intro);
			}
			String status = request.getParameter("status");
			if (ValidateUtils.isNotEmpty(status)) {
				if ("on".equals(status)) {
					systemAction.setStatus(1);
				} else {
					systemAction.setStatus(0);
				}
			}
			systemActionService.createSystemAction(systemAction);
			return ResultUtils.instJSONResult(ResultUtils.WEB_SUCCESS_CODE);

		} catch (ErrorException e) {
			e.printStackTrace();
			return ResultUtils.instJSONResult(ResultUtils.WEB_DB_SERVER_ERROR_CODE);
		}

	}

	@RequestMapping(value = "/web/systemAction/delete")
	@ResponseBody
	Object deleteSystemAction(HttpServletRequest request, HttpServletResponse response) {
		try {
			Admin detail = HeaderParser.getUser(request);
			if (detail == null) {
				throw new ErrorException(ResultUtils.WEB_DB_SERVER_ERROR_CODE);
			}
			String id = request.getParameter("id");

			SystemAction systemAction = systemActionService.searchSystemActionById(id);
			if (systemAction != null) {
				systemActionService.deleteSystyeAction(systemAction);
			}
			return ResultUtils.instJSONResult(ResultUtils.WEB_SUCCESS_CODE);

		} catch (ErrorException e) {
			e.printStackTrace();

			return ResultUtils.instJSONResult(ResultUtils.WEB_DB_SERVER_ERROR_CODE);
		}

	}

	@RequestMapping(value = "/web/systemAction/updateSave")
	@ResponseBody
	Object updateSystemAction(HttpServletRequest request, HttpServletResponse response) {

		try {

			Admin detail = HeaderParser.getUser(request);
			if (detail == null) {
				throw new ErrorException(ResultUtils.WEB_DB_SERVER_ERROR_CODE);
			}
			String id = request.getParameter("id");
			SystemAction systemActionExist = systemActionService.searchSystemActionById(id);
			if (systemActionExist != null) {
				String superId = request.getParameter("super_id");
				if (ValidateUtils.isNotEmpty(superId)) {
					systemActionExist.setSuper_id(superId);
				}
				String url = request.getParameter("url");
				if (ValidateUtils.isNotEmpty(url)) {
					systemActionExist.setUrl(url);
				}
				String name = request.getParameter("name");
				if (ValidateUtils.isNotEmpty(name)) {
					systemActionExist.setName(name);
				}
				String intro = request.getParameter("intro");
				if (ValidateUtils.isNotEmpty(intro)) {
					systemActionExist.setIntro(intro);
				}

				String status = request.getParameter("status");
				if (ValidateUtils.isNotEmpty(status)) {
					if ("on".equals(status)) {
						systemActionExist.setStatus(1);
					} else {
						systemActionExist.setStatus(0);
					}
				}

			}
			systemActionService.updateSystemAction(systemActionExist);
			return ResultUtils.instJSONResult(ResultUtils.WEB_SUCCESS_CODE);

		} catch (ErrorException e) {
			e.printStackTrace();

			return ResultUtils.instJSONResult(ResultUtils.WEB_DB_SERVER_ERROR_CODE);
		}

	}

	@RequestMapping(value = "/web/systemAction/updateStatus")
	@ResponseBody
	Object updateStatusSystemAction(HttpServletRequest request, HttpServletResponse response) {
		try {
			Admin detail = HeaderParser.getUser(request);
			if (detail == null) {
				throw new ErrorException(ResultUtils.WEB_DB_SERVER_ERROR_CODE);
			}
			String id = request.getParameter("id");
			SystemAction systemAction = systemActionService.searchSystemActionById(id);
			String status = request.getParameter("status");
			if (ValidateUtils.isNotEmpty(status)) {
				if (Integer.valueOf(status) == 1) {
					systemAction.setStatus(0);
				} else if (Integer.valueOf(status) == 1) {
					systemAction.setStatus(-1);
				}
			}
			systemActionService.updateSystemAction(systemAction);
			return ResultUtils.instJSONResult(ResultUtils.WEB_SUCCESS_CODE);
		} catch (ErrorException e) {
			e.printStackTrace();
			return ResultUtils.instJSONResult(ResultUtils.WEB_DB_SERVER_ERROR_CODE);
		}

	}

//	@RequestMapping(value = "/web/systemAction/list")
//	@ResponseBody
//	Object systemActionList(HttpServletRequest request, HttpServletResponse response) {
//		try {
//			// Admin detail = HeaderParser.getUser(request);
//			// if (detail == null) {
//			// throw new ErrorException(ResultUtils.WEB_DB_SERVER_ERROR_CODE);
//			// }
//			SystemAction systemAction = new SystemAction();
//			List<SystemAction> systemActionList = systemActionService.searchSystemActionList();
//			Map<String, List<SystemAction>> map = new HashMap<String, List<SystemAction>>();
//
//			map.put("list", systemActionList);
//			JSONResult jsonResult = ResultUtils.instJSONResult(ResultUtils.WEB_SUCCESS_CODE);
//			jsonResult.setData(map);
//			return jsonResult;
//
//		} catch (Exception e) {
//			e.printStackTrace();
//
//			return ResultUtils.instJSONResult(ResultUtils.WEB_DB_SERVER_ERROR_CODE);
//		}
//
//	}

//	@RequestMapping(value = "/web/systemAction/searchList")
//	@ResponseBody
//	Object listSystemActionBySuperId(HttpServletRequest request, HttpServletResponse response) {
//
//		try {
////			Admin detail = HeaderParser.getUser(request);
////			if (detail == null) {
////				return new ErrorException(ResultUtils.WEB_LOGIN_FAILED_CODE);
////			}
//
//			String superId = request.getParameter("superId");
//			if (!ValidateUtils.isNotEmpty(superId)) {
//				return ResultUtils.instJSONResult(ResultUtils.WEB_PARAM_ERROR_CODE);
//			}
//		//	List<SystemAction> systemActions= systemActionService.listSearchSystemActionBySuperId(superId);
//			JSONResult jsonResult = ResultUtils.instJSONResult(ResultUtils.WEB_SUCCESS_CODE);
//		//	jsonResult.setData(systemActions);
//			return jsonResult;
//			
//		} catch (ErrorException e) {
//			e.printStackTrace();
//			return ResultUtils.instJSONResult(ErrorCode.INTERNAL_SERVER_ERROR);
//		}
//
//	}

}
